package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RedisConstants;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private IUserService userService;

    @Resource
    private FollowMapper followMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        // 1. 判断到底是关注还是取关，获取当前登录的用户
        Long userId = UserHolder.getUser().getId();
        String followKey = "follows:" + userId;
        // 1.1 关注，新增数据
        if (BooleanUtil.isTrue(isFollow)){
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            boolean isSuccess = save(follow);
            if (isSuccess){
                // 把关注用户的id，放入redis的set集合sadd userId followUserId
                stringRedisTemplate.opsForSet().add(followKey,followUserId.toString());
            }
        }else {
            // 1.2 取关直接删除
            boolean isSuccess = followMapper.cancelFollow(userId, followUserId);
            //删除共同关注数据
            if (isSuccess){
                // 把关注用户的id，放入redis的set集合sadd userId followUserId
                stringRedisTemplate.opsForSet().remove(followKey,followUserId.toString());
            }
        }
        return Result.ok("操作成功");
    }


    @Override
    public Result isFollow(String followUserId) {
        Long userId = UserHolder.getUser().getId();
        // 查询是否关注
        int count=followMapper.followOrNot(userId,followUserId);
        return Result.ok(count>0);
    }

    @Override
    public Result commonFollow(Long id) {
        // 1. 获取当前登录用户
        Long userId = UserHolder.getUser().getId();
        // 2. 求交集
        String keyUserId="follows:" + userId;
        String keyId = "follows:" + id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(keyUserId, keyId);
        // 3. 解析id集合
        if (intersect == null) {
            return Result.ok(Collections.emptyList());
        }
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        // 4. 查询用户
        List<UserDTO> userDTOS = userService.listByIds(ids)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        // 5. 返回这个用户集合
        return Result.ok(userDTOS);
    }
}
